/*
 * (c) ralfoide, http://gamez.googlecode.com/, 2008
 * Project: gamez
 * License TBD
 */

package com.alfray.gamez.gameplay;

import java.io.InputStream;
import java.io.InputStreamReader;

import javax.microedition.khronos.opengles.GL10;

import util.BlenderTextFileReader;
import util.BlenderTextFileReader.ParseError;
import android.util.Log;
import android.view.KeyEvent;

import com.alfray.gamez.R;
import com.alfray.gamez.GamezActivity.Context;
import com.alfray.gamez.GamezActivity.GameplayId;
import com.alfray.gamez.shape.FileRB1;
import com.alfray.gamez.shape.Shape;
import com.alfray.gamez.shape.Square;

//-----------------------------------------------

public class Shooter extends Gameplay {

	private static String TAG = "Gamez.Shooter";
	
    private static final int NUM_CELL_WIDTH = 8;
    private static final int NUM_CELL_HEIGHT = 8;

	private FileRB1 mSprite;

    public Shooter(Context context) {
    	super(context);
    	setUseLighting(false);
    	setRenderSprites(true);
    	setUseTexture(true);

    	InputStream is = context.mAndroidContext.getResources().openRawResource(R.raw.diamond1);
    	InputStreamReader isr = new InputStreamReader(is);
    	BlenderTextFileReader blender;
		try {
			blender = new BlenderTextFileReader(isr);
	    	mSprite = new FileRB1(blender.getObjects().get(0), Shape.Color.GREEN);
		} catch (ParseError e) {
			Log.e(TAG, "Loading blender diamond1 failed", e);
		}
	}

    @Override
	public String getMessage() {
		return "Game!z shooter, ready!";
	}

    @Override
    public void setupScene(GL10 gl, int w, int h) {
    	super.setupScene(gl, w, h);
    	// tell sprite to use lighting depending on current scene attributes
    	mSprite.setUseLighting(mUseLighting);
    	mSprite.setEnable(mRenderSprites);

    	mSprite.setUseTextureUnit(1);
    	associateTexture(gl, GL10.GL_TEXTURE0 /* TMU1 */, 2);
    	loadTexture(gl, R.raw.sprite2, GL10.GL_RGB, 2);
    }

    /**
     * Draws one frame on the GL animation.
     * 
     * Called for every frame.
     */
    @Override
    public void drawScene(GL10 gl, int w, int h) {
    	super.drawScene(gl, w, h);
    	/*
    	final int p05 = 1<< (16-1);
        final int cw = NUM_CELL_WIDTH;
        final int ch = NUM_CELL_HEIGHT;
        final int cw1 = cw << 16;
        final int cw2 = cw1 >> 1;
        final int ch2 = NUM_CELL_HEIGHT << (16-1);
        gl.glPushMatrix();

        long ms_since_start = System.currentTimeMillis() - mContext.mStartTime;
        gl.glRotatex((int)(ms_since_start << (16-2)), 0, 0, 1 << 16);

        // gl.glTranslatef(-0.5f * w - 0.5f, -0.5f * h - 0.5f, 0.0f);
        gl.glTranslatex(0 - cw2 - p05, 0 - ch2 -  p05, 0);
        
        for (int j = 0; j < ch; j++) {
            for (int i = 0; i < cw; i++) {
            	mSprite.drawTo(gl);
            	// gl.glTranslatef(1, 0, 0);
                gl.glTranslatex(1 << 16, 0, 0);
            }
            // gl.glTranslatef(0.0f - w, 1.0f, 0.0f);
            gl.glTranslatex(0 - cw1, 1 << 16, 0);
        }
        gl.glPopMatrix();
        */
    	mSprite.drawTo(gl);
    }
    
    @Override
    public int threadUpdate(Context context) {
    	return 100; // sleep 100 ms
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK) {
    		return mContext.switchGameplay(GameplayId.Title);
    	}
    	return super.onKeyDown(keyCode, event);
    }
}

